package org.example.springboot_threadpool_demo.taskpool.reject;

import org.example.springboot_threadpool_demo.taskpool.task.ComputeTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

@Primary
@Component
public class LogDbRejectPolicy implements RejectedExecutionHandler {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        if (r instanceof ComputeTask) {
            System.out.println("reject task");
            ComputeTask t = (ComputeTask) r;
            String sql = "insert into task_log_info (group_name, data) values (?, ?)";
            int update = jdbcTemplate.update(sql, t.getGroupName(), t.getData());
            System.out.println("compute task reject: " + t.getGroupName());
        }

    }
}
